package com.eliasbalasis.dwr4gwt.example;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Random;

import org.directwebremoting.annotations.RemoteMethod;
import org.directwebremoting.annotations.RemoteProxy;

import com.eliasbalasis.dwr4gwt.example.Person;

@RemoteProxy
public class RemoteFunctions {
	private static Publisher publisher = new Publisher();
	private static Random random = new Random();
	private static long DELAY = 2000;

	@RemoteMethod
	public boolean getBoolean() {
		try {
			Thread.sleep(DELAY);
		} catch (InterruptedException e) {
		}
		return random.nextBoolean();
	}

	@RemoteMethod
	public double getNumber() {
		try {
			Thread.sleep(DELAY);
		} catch (InterruptedException e) {
		}
		return random.nextDouble();
	}

	@RemoteMethod
	public String getString() {
		try {
			Thread.sleep(DELAY);
		} catch (InterruptedException e) {
		}
		return "String "+new Long(random.nextLong()).toString();
	}

	@RemoteMethod
	public Collection<Person> getArray() {
		Collection<Person> persons = new ArrayList<Person>();
		Person person1 = new Person("person1","This is person 1");
		Person person2 = new Person("person2","This is person 2");
		Person person3 = new Person("person3","This is person 3");
		persons.add(person1);
		persons.add(person2);
		persons.add(person3);
		try {
			Thread.sleep(DELAY);
		} catch (InterruptedException e) {
		}
		return persons;
	}

	@RemoteMethod
	public Person getObject() {
		try {
			Thread.sleep(DELAY);
		} catch (InterruptedException e) {
		}
		return new Person("anObject","This is an object");
	}

	@RemoteMethod
	public void doWithParameters(
			boolean paramBoolean,
			double paramDouble,
			String paramString,
			Collection<Person> paramCollection,
			Person paramObject) {
		try {
			Thread.sleep(DELAY);
		} catch (InterruptedException e) {
		}
	}
	@RemoteMethod
	public String sayHello(String name) {
		//try {
		//	Thread.sleep(DELAY);
		//} catch (InterruptedException e) {
		//}
		return "Hello "+name;
	}

}
